﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Xin478.CSWork.Client
{
    public class MyForm : Form
    {
        Point mouseOff;//鼠标点击标题栏时的偏移量
        bool leftFlag;//鼠标点击标题栏时是否为左键
        PictureBox maxpb, minpb, closepb;
        bool allowMove = true;
        Label formTextLabel = new Label();
        int Radius = 6;
        const int titleBarHeight = 23;

        public MyForm()
            : base()
        {
            Padding padding = new Padding();
            padding.All = 2;
            //padding.Top = titleBarHeight;
            this.Padding = padding;

            Color color = Color.FromArgb(255, 228, 0);
            this.TransparencyKey = color;
            this.BackColor = color;
            SetStyles();
        }

        /*
        Rectangle maxBtnRect=new Rectangle(0,3,30,20);
        Rectangle minBtnRect = new Rectangle(0, 3, 30, 20);
        Rectangle closeBtnRect = new Rectangle(0, 3, 30, 20);
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (leftFlag) leftFlag = false;//释放鼠标后标注为false;
            base.OnMouseUp(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (this.AllowMove && leftFlag && mouseOff != Point.Empty)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
            base.OnMouseMove(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this.AllowMove && e.Y <= titleBarHeight)
                    mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                else
                    mouseOff = Point.Empty;
                leftFlag = true;                  //点击左键按下时标注为true;
            }
            base.OnMouseDown(e);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            minBtnRect.X = ClientRectangle.Width - 99;
            maxBtnRect.X = ClientRectangle.Width - 68;
            closeBtnRect.X = ClientRectangle.Width - 37;
        }
        */

        //protected bool allowUserResize = true;
        //bool leftdown=false;
        //int border = 10;
        public bool AllowMove { get { return allowMove; } set { allowMove = value; } }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                formTextLabel.Text = value;
                base.Text = value;
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            Graphics graphics = pe.Graphics;
            /*
            if (this.ControlBox)
            {
                if (pe.ClipRectangle == closeBtnRect)
                {
                    graphics.DrawImage(MainFormResource.关闭激活, closeBtnRect);
                    return;
                }
                else if (pe.ClipRectangle == minBtnRect)
                {
                    graphics.DrawImage(MainFormResource.最小化激活, minBtnRect);
                    return;
                }
                else if (pe.ClipRectangle == maxBtnRect)
                {
                    graphics.DrawImage(this.WindowState == FormWindowState.Normal ? MainFormResource.最大化激活 : MainFormResource.正常化激活, maxBtnRect);
                    return;
                }

            }
             */
            Rectangle rect = new Rectangle(0, 0, ClientSize.Width , ClientSize.Height );           

            using (GraphicsPath path = new GraphicsPath())
            {
                path.AddArc(rect.X , rect.Y, Radius, Radius, 180F, 90F);
                path.AddArc(rect.Right-Radius-1 , rect.Y , Radius, Radius, 270F, 90F);
                path.AddArc(rect.Right-Radius-1, rect.Bottom-Radius-1, Radius, Radius, 0F, 90F);
                path.AddArc(rect.X, rect.Bottom-Radius-1, Radius, Radius, 90F, 90F);
                path.CloseFigure();                
                using (Brush tmpb = new SolidBrush(Color.FromArgb(105, 166, 242)))
                {
                    graphics.FillPath(tmpb, path);
                }
                using (LinearGradientBrush lg = new LinearGradientBrush(new Rectangle(0, 0,rect.Width, 24), Color.FromArgb(104, 163, 237), Color.FromArgb(141,188,248), LinearGradientMode.Vertical))
                {
                    using (GraphicsPath path1 = new GraphicsPath())
                    {
                        path1.AddArc(rect.X, rect.Y, Radius, Radius, 180F, 90F);
                        path1.AddArc(rect.Right - Radius - 1, rect.Y, Radius, Radius, 270F, 90F);
                        path1.AddLine(rect.Right  - 1, rect.Y- Radius / 2, rect.Right - 1, rect.Y + 23);
                        path1.AddLine(rect.Right  - 1, rect.Y + 23, rect.Left,rect.Y+23);
                        path.CloseFigure();
                        graphics.FillPath(lg, path1);
                    }
                }
                using (Pen p = new Pen(Color.FromArgb(0, 46, 107), 1))
                {
                    graphics.DrawPath(p, path);
                    p.Color = Color.FromArgb(194, 218, 248);
                    graphics.DrawLine(p, Radius / 2, 1, rect.Right - Radius/2-1, 1);
                }
            }
            /*
            if (this.ControlBox)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.X -= this.Location.X;
                mouseSet.Y -= this.Location.Y;
                if (this.MinimizeBox)
                {
                    if (mouseSet.Y > 3 && mouseSet.Y < 23 && mouseSet.X < ClientSize.Width - 69 && mouseSet.X > ClientSize.Width - 99)
                        graphics.DrawImage(MainFormResource.最小化激活, ClientSize.Width - 99,3,30,20);
                    else
                        graphics.DrawImage(MainFormResource.最小化,  ClientSize.Width - 99,3);
                }

                if (this.MaximizeBox)
                {
                    if (mouseSet.Y > 3 && mouseSet.Y < 23 && mouseSet.X < ClientSize.Width - 38 && mouseSet.X > ClientSize.Width - 68)
                        graphics.DrawImage(this.WindowState == FormWindowState.Normal ? MainFormResource.最大化激活 : MainFormResource.正常化激活,  ClientSize.Width - 68,3);
                    else
                        graphics.DrawImage(this.WindowState == FormWindowState.Normal ? MainFormResource.最大化 : MainFormResource.正常化,  ClientSize.Width - 68,3);
                }

                if (mouseSet.Y > 3 && mouseSet.Y < 23 && mouseSet.X < ClientSize.Width - 7 && mouseSet.X > ClientSize.Width - 37)
                    graphics.DrawImage(MainFormResource.关闭激活, ClientSize.Width - 37,3);
                else
                    graphics.DrawImage(MainFormResource.关闭, ClientSize.Width - 37,3);
            }
            */
        }

        protected override void OnShown(EventArgs _e)
        {
            base.OnShown(_e);
            this.FormBorderStyle = FormBorderStyle.None;
            
            Panel panel = new Panel();
            panel.Dock = DockStyle.Top;
            panel.Size = new Size(100, titleBarHeight-2);
            panel.BackColor = Color.Transparent;

            formTextLabel.ForeColor = Color.White;
            formTextLabel.BackColor = Color.Transparent;
            formTextLabel.Location = new Point(6, 5);
            formTextLabel.Size = new Size(10, 10);
            formTextLabel.AutoSize = true;
            formTextLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)));
            panel.Controls.Add(formTextLabel);

            if (this.ControlBox)
            {
                if (this.MinimizeBox)
                {
                    minpb = new PictureBox();
                    minpb.BackColor = Color.Transparent;
                    minpb.Image = MainFormResource.最小化;
                    minpb.Height = 20;
                    minpb.Width = 30;
                    minpb.Location = new Point(0, 1);
                    minpb.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
                    minpb.TabStop = false;
                    minpb.Dock = DockStyle.None;
                    minpb.MouseHover += new EventHandler(delegate(object sender, EventArgs e)
                    {
                        minpb.Image = MainFormResource.最小化激活;
                    });
                    minpb.MouseLeave += new EventHandler(delegate(object sender, EventArgs e)
                    {
                        minpb.Image = MainFormResource.最小化;
                    });
                    minpb.MouseClick += new MouseEventHandler(delegate(object sender, MouseEventArgs e)
                    {
                        this.WindowState = FormWindowState.Minimized;
                    });
                    panel.Controls.Add(minpb);
                }

                if (this.MaximizeBox)
                {
                    maxpb = new PictureBox();
                    maxpb.BackColor = Color.Transparent;
                    maxpb.Image = MainFormResource.最大化;
                    maxpb.Height = 20;
                    maxpb.Width = 30;
                    maxpb.Dock = DockStyle.None;
                    maxpb.Location = new Point(34, 1);
                    maxpb.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));

                    maxpb.MouseHover += new EventHandler(delegate(object sender, EventArgs e)
                    {
                        maxpb.Image = this.WindowState == FormWindowState.Normal ? MainFormResource.最大化激活 : MainFormResource.正常化激活;
                    });
                    maxpb.MouseLeave += new EventHandler(delegate(object sender, EventArgs e)
                    {
                        maxpb.Image = this.WindowState == FormWindowState.Normal ? MainFormResource.最大化 : MainFormResource.正常化;
                    });
                    maxpb.MouseClick += new MouseEventHandler(maxpb_MouseClick);
                    panel.Controls.Add(maxpb);

                    panel.MouseDoubleClick += new MouseEventHandler(maxpb_MouseClick);
                }

                closepb = new PictureBox();
                closepb.BackColor = Color.Transparent;
                closepb.Image = MainFormResource.关闭;
                closepb.Height = 20;
                closepb.Width = 30;
                closepb.Location = new Point(67, 1);
                closepb.Dock = DockStyle.None;
                closepb.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
                closepb.MouseHover += new EventHandler(delegate(object sender, EventArgs e)
                {
                    closepb.Image = MainFormResource.关闭激活;
                });
                closepb.MouseLeave += new EventHandler(delegate(object sender, EventArgs e)
                {
                    closepb.Image = MainFormResource.关闭;
                });
                closepb.MouseClick += new MouseEventHandler(delegate(object sender, MouseEventArgs e)
                {
                    this.Close();
                });
                panel.Controls.Add(closepb);
            }

            this.Controls.Add(panel);
            panel.MouseDown+=new MouseEventHandler(delegate(object sender, MouseEventArgs e){
                if (e.Button == MouseButtons.Left)
                {
                    if (this.AllowMove && e.Y <= titleBarHeight)
                        mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                    else
                        mouseOff = Point.Empty;
                    leftFlag = true;                  //点击左键按下时标注为true;
                }
            });
            panel.MouseMove+=new MouseEventHandler(delegate(object sender, MouseEventArgs e){
                if (this.AllowMove && leftFlag && mouseOff != Point.Empty)
                {
                    Point mouseSet = Control.MousePosition;
                    mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                    Location = mouseSet;
                }
            });
            panel.MouseUp += new MouseEventHandler(delegate(object sender, MouseEventArgs e)
            {
                if (leftFlag) leftFlag = false;//释放鼠标后标注为false;
            });

            /*
            if (this.allowUserResize)
            {
                this.MouseMove += new MouseEventHandler(MyForm_MouseMove);
                this.MouseLeave += new EventHandler(MyForm_MouseLeave);
                this.MouseDown+=new MouseEventHandler(MyForm_MouseDown);
            }*/
        }

        /*
        void MyForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                leftdown = true;
                mouseOff = new Point(e.X, e.Y);

            }
            else
                leftdown = false;
        }

        void MyForm_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Arrow;
            leftdown=false;
        }

        void MyForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftdown)
            {
                if (this.Cursor != Cursors.Arrow)
                {
                    this.Invalidate();
                }
            }
            else
            {
                this.Cursor = CheckCursorType(e.X, e.Y);
            }
        }
        private Cursor CheckCursorType(int X, int Y)
        {
            if (((X <= border + 10 && Y <= border) || (Y <= border + 10 && X <= border))
                || ((X >= Width - border - 10 && Y >= Height - border)
                || (Y >= Height - border - 10 && X >= Width - border)))
            {
                return Cursors.SizeNWSE;
            }
            else if (((Y <= border + 10 && X >= Width - border)
                || (Y <= border && X >= Width - border - 10))
                || ((X <= border && Y >= Height - border - 10)
                || (Y >= Height - border && X <= border + 10)))
            {
                return Cursors.SizeNESW;
            }
            else if (X >= Width - border)
            {
                return Cursors.SizeWE;
            }
            else if(X <= border)
            {
                return Cursors.SizeWE;
            }
            else if (Y >= Height - border)
            {
                return Cursors.SizeNS;
            }
            else if ( Y <= border)
            {
                return Cursors.SizeNS;
            }
            else
            {
                return Cursors.Arrow;
            }
        }
        */

        void maxpb_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this.WindowState == FormWindowState.Normal)
                {
                    this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                    this.WindowState = FormWindowState.Maximized;
                    maxpb.Image = MainFormResource.正常化;
                }
                else
                {
                    this.WindowState = FormWindowState.Normal;
                    maxpb.Image = MainFormResource.最大化;
                }
            }
        }

        private void SetStyles()
        {
            SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.ResizeRedraw, true);
            UpdateStyles();
        }
    }
}
